# encoding: utf-8  
'''
Created on 2018年6月14日
description:脚本执行类,执行../scripts/目录下的所有shell脚本和python脚本
@author: zhujianhua
'''

import os
import stat
import sys

sys.path.append('./helper')
from LoggerHelper import LoggerHelper

class ExecuteScripts:
    logger = None
    
    def __init__(self):
        self.logger = LoggerHelper("ExecuteScripts")
    
    #执行python脚本
    def executePython(self,fileName):
        self.logger.info("execute " + fileName)
        os.chmod(fileName, stat.S_IXUSR)#给拥有者授可执行权限
        command = 'python ' + fileName
        outPut = os.popen(command).read()#执行python脚本
        
        
    #执行shell脚本
    def executeShell(self,fileName):
        self.logger.info("execute " + fileName)
        os.chmod(fileName, stat.S_IXUSR)#给拥有者授可执行权限
        command = 'sh ' + fileName
        outPut = os.popen(command).read()#执行shell脚本
    
    #执行入口
    def execute(self):
        #获取./scripts/目录下的所有文件
        absPath = os.path.abspath('./scripts/') 
        #获取/scripts/文件夹的绝对路径
        scriptFileList = os.listdir('./scripts/')
        
        for fileName in scriptFileList:
            if fileName.endswith('.py'):#匹配python脚本
                self.executePython(absPath + '/' +fileName)
            elif fileName.endswith('.sh'):#匹配shell脚本
                self.executeShell(absPath + '/' +fileName)
            else:
                continue
            
            